package com.medical.data.config;


import com.medical.data.component.MyInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Component
@Slf4j
public class OtherWebConfig implements WebMvcConfigurer {

    @Value("${upload.resource-addr}")
    private String path;

    @Autowired
    private MyInterceptor myInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor).addPathPatterns("/**"); //任意请求都需要进行拦截
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("=======>{}",path);
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+path);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
                .allowedOrigins("*")    //允许跨域
                .allowedHeaders("*")   //所有的请求都可以加入头信息
                .allowedMethods("POST","GET","PUT","DELETE")  //允许的请求方式
                .maxAge(36000);    //预检测时间
        //.allowCredentials(true);  //允许发送cookie
    }
}
